import path from 'path'
import { fileURLToPath } from 'url'
import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import WindiCSS from 'vite-plugin-windicss'
import DefineOptions from 'unplugin-vue-define-options/vite'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { NaiveUiResolver } from 'unplugin-vue-components/resolvers'
import { VantResolver } from 'unplugin-vue-components/resolvers';
import requireToUrlPlugin from './src/plugins/require/index.js'; // 无效插件，仅参考vite插件的写法

import vueJsx from '@vitejs/plugin-vue-jsx'
import tsxResolveTypes from 'vite-plugin-tsx-resolve-types'


// https://vitejs.dev/config/
// export default defineConfig({
//   server: {
//     open: '/index.html',
//     host: '0.0.0.0',
//     proxy: {
//       '/api': {
//         // target: 'http://192.168.10.20:30500',
//         target: 'http://www.tochainlan.com/api/app',
//         // target: 'https://x617498z35.oicp.vip',

//         changeOrigin: true,
//         rewrite: path => path.replace(/^\/api/, ''),
//       },
//     },
//   },

//   plugins: [
//     vue(),
//     WindiCSS(),
//     DefineOptions(),
//     AutoImport({
//       imports: [
//         'vue',
//         'vue/macros',
//         'vue-router',
//         'vue-i18n',
//         '@vueuse/core',
//         'pinia',
//         {
//           'naive-ui': ['useDialog', 'useMessage', 'useNotification', 'useLoadingBar'],
//         },
//       ],
//       // dts: 'types/auto-imports.d.ts',
//       dirs: ['src/stores', 'src/composables', 'src/api'],
//     }),
//     Components({
//       resolvers: [NaiveUiResolver()],
//       // dts: 'types/components.d.ts',
//       dirs: ['src/components'],
//     }),

//   ],
//   base: process.env.NODE_ENV === 'development' ? '' : '/tzl-app/',
//   resolve: {
//     alias: {
//       '~': path.resolve(__dirname, 'src'),
//       '@': path.resolve(__dirname, 'src'),
//     },
//   },

// })

const baseSrc = fileURLToPath(new URL('./src', import.meta.url))
export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd())

  return {

    build: {
      chunkSizeWarningLimit: 2000,
      rollupOptions: {
        output: {
          manualChunks: {
            naiveUI: ['naive-ui'],
            // antdIcon: ['@vicons/antd'],
            lodash: ['lodash-es', '@vueuse/core'],
            vue: ['vue', 'vue-router', 'vue-i18n', 'pinia'],
          },
        },
      },
      minify: 'terser',
      terserOptions: {
        compress: {
          drop_console: true,
          drop_debugger: true
        }
      }
    },
    server: {
      open: '/index.html',
      host: '0.0.0.0',
      port: 8888,
      proxy: {
        [env.VITE_APP_BASE_API]: {
          target: env.VITE_APP_BASE_URL,
          changeOrigin: true,
          ws: false,
          rewrite: path => path.replace(new RegExp(`^${env.VITE_APP_BASE_API}`), ''),
        },
      },
    },
    define: {
      __VUE_I18N_FULL_INSTALL__: false,
      __VUE_I18N_LEGACY_API__: false,
      __VUE_PROD_HYDRATION_MISMATCH_DETAILS__: true,
      // VUE_I18N_FULL_INSTALL: true,
      // VUE_I18N_LEGACY_API: true,
      // INTLIFY_PROD_DEVTOOLS: false
    },
    resolve: {
      dedupe: [
        'vue'
      ],
      alias: {
        '~': baseSrc,
        '~@': baseSrc,
        '@': baseSrc,
      },
    },
    plugins: [
      vue({ template: { precompileRoot: false } }),
      vueJsx(),
      tsxResolveTypes(),
      WindiCSS(),
      DefineOptions(),
      requireToUrlPlugin(),
      AutoImport({
        imports: [
          'vue',
          'vue/macros',
          'vue-router',
          'vue-i18n',
          '@vueuse/core',
          'pinia',
          {
            'naive-ui': ['useDialog', 'useMessage', 'useNotification', 'useLoadingBar'],
          },
          {
            'vant': ['Button']
          },
        ],
        dts: 'types/auto-imports.d.ts',
        dirs: ['src/store', 'src/composables', 'src/api'],
      }),
      Components({
        resolvers: [NaiveUiResolver(), VantResolver()],
        dts: 'types/components.d.ts',
        dirs: ['src/components'],
      }),
    ],
  }
})
